package org.infodavid.common.services;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IDocumentVersionListItem;
import org.infodavid.common.impl.model.criteria.DocumentVersionCriteriaImpl;
import org.infodavid.common.model.AbstractBeanTest;
import org.infodavid.common.model.DocumentVersionTest;
import org.infodavid.common.model.IDocumentVersion;
import org.infodavid.common.model.criteria.IDocumentVersionCriteria;
import org.infodavid.common.persistence.DocumentDataService;
import org.infodavid.common.services.DocumentService;
import org.infodavid.common.services.exception.IllegalOperationException;

/**
 * The Class DocumentServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class DocumentServiceTest extends
    AbstractServiceTest<IDocumentVersionListItem,IDocumentVersion,IDocumentVersionCriteria,DocumentDataService> {

	/** The delegate. */
	private final DocumentVersionTest delegate = new DocumentVersionTest(this);

	/**
	 * The Constructor.
	 */
	public DocumentServiceTest() {
		super(DocumentService.getInstance());
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @return the data service class
	 * @see org.infodavid.common.services.AbstractServiceTest#getDataServiceClass()
	 */
	@Override
	public Class<DocumentDataService> getDataServiceClass() {
		return DocumentDataService.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractServiceTest#buildCriteria()
	 */
	@Override
	public IDocumentVersionCriteria buildCriteria() {
		final IDocumentVersionCriteria criteria = new DocumentVersionCriteriaImpl();

		criteria.setNamePattern("name");

		return criteria;
	}

	/**
	 * Test find by name.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByName() throws Exception {
		final IDocumentVersion value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findDocumentByName(getContextMock(), value.getName());
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNull("An exception occurs", exception);
		Assert.assertNotNull("Data is null", value);
		Assert.assertNotNull("Key is null", value.getKey());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameWithoutValue() throws Exception {
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findDocumentByName(getContextMock(), null);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/**
	 * Test testFindByName.
	 * @throws Exception the exception
	 */
	@Test
	public final void testFindByNameUsingAnInvalidContext() throws Exception {
		final IDocumentVersion value = populate(ITEMS_TO_INSERT).get(0);
		Exception exception = null;

		expectationsOnPersistenceSession();

		try {
			((DocumentService)getService()).findDocumentByName(null, value.getName());
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractServiceTest#testRemove()
	 */
	@SuppressWarnings("boxing")
  @Override
  @Test
	public void testRemove() throws Exception {
		final IDocumentVersion value = populate(ITEMS_TO_INSERT).get(ITEMS_TO_INSERT - 1);
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().remove(getContextMock(), value.getKey());
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalOperationException.class, exception.getClass());
	}

	/* (non-Javadoc)
	 * @see org.infodavid.common.services.AbstractServiceTest#testRemoveUsingAnInvalidKey()
	 */
	@Override
	@Test
	public void testRemoveUsingAnInvalidKey() throws Exception {
		Exception exception = null;

		expectationsOnPersistenceSession();
		expectationsOnSecureService();

		try {
			getService().remove(getContextMock(), -1);
		}
		catch (final Exception e) {
			e.printStackTrace();
			exception = e;
		}

		Assert.assertNotNull("An exception occurs", exception);
		Assert.assertEquals("Wrong exception", IllegalOperationException.class, exception.getClass());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.persistence.AbstractDataServiceTest#getBeanTest()
	 */
	@Override
	protected AbstractBeanTest<IDocumentVersion> getBeanTest() {
		return delegate;
	}
}
